<?php
if ( $_SESSION['logged_in'] == FALSE ) redirect (base_url ());
echo '<div id="contentfull">';
echo '<div id="forms">';
//print_r($roles);
$name = $data[0]['Name'];
$username = $data[0]['username'];
//var_dump($username);
//echo validation_errors('<div class="error">', '</div>'); 
//$attributes = array('class' => 'frmedit', 'width' => '600px', 'margin' => '0 auto');
$hidden = array('id' => $uid);
$opt = array('name' => 'edituser', 'id' => 'edituser');
echo '<a href="'.$baseurl.'index.php/users"><img class="btnimg" src="'.$baseurl.'public/images/close.png" align="right" alt="close" border="0" /></a>';
echo form_open($baseurl.'index.php/edituser/'.$uid, $opt ,$hidden);
echo form_fieldset('<h2>   User Edit / Update   </h2>');


?>
 
<!--
TODO  -- Add password encryption
TODO  -- Allow adding and subtracting of areas
TODO  -- Allow adding and Subtracting of Groups
-->

<table>
    <tr>
        <td width="150px" >
<?php   echo form_label('Name *', 'Name'); 
        //echo form_error('Name'); 
        $namec = array('name' => 'Name', 
               'id' => 'Name',  
               'maxlength'   => '100',
               'value'       => $data[0]['Name'],
              'style'       => 'width: 200px' ); ?>  </td>
        <td width="200px" ><?php echo form_input($namec,'' ,'onblur="chkpubname()"'); echo '<br>'.form_error('Name');?>       </td>
        <td width="150px" ></td>
        <td width="200px" ><?php echo $data[0]['_id']; ?></td>
    </tr>
    <tr>
        <td width="150px" >
<?php   echo form_label('Public Name *', 'PublicName'); 
        //echo form_error('Name'); 
        $pnamec = array('name' => 'PublicName', 
               'id' => 'PublicName',  
               'maxlength'   => '100',
               'value'       => $data[0]['PublicName'],
              'style'       => 'width: 200px' ); ?>  </td>
        <td width="200px" ><?php echo form_input($pnamec); echo '<br>'.form_error('PublicName');?>       </td>
        <td width="150px" ></td>
        <td width="200px" ></td>
    </tr>
    <tr>
        <td> <?php echo form_label('Email *', 'username'); 
            $usernamec = array('name' => 'username', 
            'id' => 'username',  
            'maxlength'   => '100',
            'value'       => $data[0]['username'],
            'style'       => 'width: 200px' ); ?> </td> 
        <td> <?php echo form_input($usernamec); echo '<br>'.form_error('username'); ?> </td>
        <td> <?php echo form_label('Verified', 'verified'); $verifiedc = array('name' => 'verified', 
               'id' => 'verified',  
              'value'       => $data[0]['verified'], 
              'checked'     => $data[0]['verified']);
        
        ?> </td>
        <td> <?php echo form_checkbox($verifiedc); ?> </td>
    </tr>
    <tr>
        <td> <?php echo form_label('Mobile', 'mobile'); $mobilec = array('name' => 'mobile', 
               'id' => 'mobile',  
               'maxlength'   => '10',
              'value'       => $data[0]['mobile'],
              'style'       => 'width: 100px' ); ?> </td>
        <td> <?php echo form_input($mobilec); echo '<br>'.form_error('mobile'); ?> </td>
        <td> <?php echo form_label('Active', 'active');  $activec = array('name' => 'active', 
               'id' => 'active',  
              'value'       => $data[0]['active'], 
              'checked'     => $data[0]['active']);
        
        ?> </td>
        <td> <?php echo form_checkbox($activec);  ?> </td>
    </tr>
    <tr>
        <td> <?php echo form_label('Security Level *', 'level'); $level = array('name' => 'level', 'id' => 'level',  
              'maxlength'   => '2',
              'value'       => $data[0]['level'],
              'style'       => 'width: 30px' ); ?> </td>
        <td> <?php echo form_input($level); echo '<br>'.form_error('level'); ?> </td>
         <td> <?php echo form_label('Date on System', '');  ?> </td>
        <td> <?php echo date('d M Y H:i:s', $data[0]['created']->sec); ?> </td>
    </tr>
     <tr>
        <td> <?php echo form_label('Role', 'role'); $level = array('name' => 'role', 'id' => 'role' ); 
        $options = [];
        foreach($roles as $value)
        {
            $t = array($value['_id'] => $value['Display']);
            $options = array_merge($options, $t); 
        }
        $selected = $data[0]['role'];
        //print_r($options);
        ?> </td>
        <td> <?php echo form_dropdown('role', $options, $selected); ?> </td>
    </tr>
    <tr>
        <td> <?php echo form_label('Linked to Area', 'areas');  $level = array('name' => 'areas', 'id' => 'areas' );
        
        // This code allows for all areas to be displayed and the ones that are linked to be selected
//        $options = [];
//        foreach($areas as $value)
//        {
//            $mid = $value['id'] ;
//            $t = array($mid => $value['Area']);
//            $options = array_merge($options, $t); 
//        }
//      
//        $selected = [];
//        foreach ($data[0]['areas'] as $sel) {
//            $selected = array_merge($selected, $sel);
//        }

        // This chunk of code only shows the arears that are linked
         $options = [];
        // var_dump($data[0]['areas']);
         foreach($data[0]['areas'] as $value)
        {
            $mid = $value['area'] ;
            $t = array($mid => $value['areaname']);
            $options = array_merge($options, $t); 
        }
        
        
?> </td>
        <td> <?php echo form_multiselect('areas', $options); ?> </td>
<!--    </tr>-->
<!--    <tr>-->
        <td> <?php echo form_label('Linked to Groups', 'grp'); $level = array('name' => 'grp', 'id' => 'grp',  'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width: 150px',); 
   //     ===================
        // This code allows for all groups to be displayed and the ones that are linked to be selected
//        $options = [];
//        foreach($groups as $value)
//        {
//            $mid = $value['id'] ;
//            $t = array($mid => $value['GroupName']);
//            $options = array_merge($options, $t); 
//        }
//      
//        $selected = [];
//        foreach ($data[0]['grp'] as $sel) {
//            $selected = array_merge($selected, $sel);
//        }
        // This chunk of code only shows the groups that are linked
        $options = [];
         foreach($data[0]['grp'] as $value)
        {
            $mid = $value['grpid'] ;
            $t = array($mid => $value['grpname']);
            $options = array_merge($options, $t); 
        }
 
      
        ?> </td>
        <td> <?php echo form_multiselect('grp', $options); ?> </td>
    </tr>
    <tr>
        <td colspan="4" align="right" > <?php echo form_submit('save', 'Save');
        echo form_reset('reset', 'Reset');
        echo form_submit('delete', 'Delete');
        echo form_submit('chpass', 'Change Password');?>  
        
    </tr>
</table>    
<?=form_fieldset_close()?>
<?=form_close();?>

</div> <!--content-->
</div> <!--login-->

<script language="javascript" type="text/javascript">
    
function chkpubname() {
      var pubname = document.getElementById("PublicName").value;
      var uname =  document.getElementById("Name").value 
      //alert(uname);
      //alert(pubname.length);
      if (pubname.length == 0) {
         
         document.getElementById("PublicName").value = uname ;
       //  document.getElementById("PublicName").text = uname ;
      }
     // Now do an Ajax call to check if Public name is free
}

function ChPubNameUnique(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
        
        // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){ 
		// (Complete) Get the data from the server's response
              //  document.prereg.email.value = ajaxRequest.responseText;
                var result = ajaxRequest.responseText;
                var n=result.slice(-1);
                if (n == '1') {
                    var msg = '';
                    document.getElementById("email").innerHTML = msg;
                } else {
                    var msg = 'Email address already registered please use another address';
                    document.getElementById("username").focus();
                    document.getElementById("email").innerHTML = msg;
                }
                
                    //ajaxRequest.responseText;
            }
        }
        // Send the Request to the server
        var pubname = document.getElementById("PublicName").value;
        ajaxRequest.open("GET", "checkpubnamel?pname="+pubname  , true);
        ajaxRequest.send();
}

    
</script> 
